<?php

namespace app\asking\controller;

use think\Controller;

class Post extends Controller
{
    public function __construct(){
        parent::__construct();

        $this->BusinessModel = model('Business.User');
        $this->PostModel = model('Post.Post');
        $this->CateModel = model('Post.Cate');
        $this->CollectModel = model('Post.Collect');
        $this->CommentModel = model('Post.Comment');
    }


    // 查询帖子分类
    public function cate(){
        $list = $this->CateModel->order('weigh','asc')->select();

        if($list){
            $this->success('返回帖子分类',null,$list);
            exit;
        }else{
            $this->error('暂无帖子分类');
            exit;
        }
    }


    // 发帖
    public function add(){
        if($this->request->isPost()){
            $cateid = $this->request->param('cateid',0,'trim');
            $content = $this->request->param('content','','trim');
            $point = $this->request->param('point',0,'trim');
            $title = $this->request->param('title','','trim');
            $busid = $this->request->param('busid',0,'trim');

            // var_dump($this->request->param());
            // exit;

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询用户积分是否充足
            $Updatepoint = bcsub($business['point'],$point);
            if($Updatepoint < 0){
                $this->error('积分不足，请充值');
                exit;
            }

            // 查询分类
            $cate = $this->CateModel->find($cateid);
            if(!$cate){
                $this->error('分类信息未知');
                exit;
            }

            // 开启事务
            $this->PostModel->startTrans();
            $this->BusinessModel->startTrans();

            // 组装帖子数据
            $PostData = [
                'title' => $title,
                'content' => $content,
                'point' => $point,
                'status' => 0,
                'busid' => $busid,
                'cateid' => $cateid
            ];

            $PostStatus = $this->PostModel->validate('common/Post/Post')->save($PostData);
            if($PostStatus === FALSE){
                $this->error($this->PostModel->getError());
                exit;
            }

            // 更新用户积分
            $BusinessData = [
                'id' => $busid,
                'point' => $Updatepoint,
                'deal' => 1
            ];

            $BusinessStatus = $this->BusinessModel->isUpdate(true)->save($BusinessData);
            if($BusinessStatus === FALSE){
                $this->PostModel->rollback();
                $this->error($this->BusinessModel->getError());
            }

            if($PostStatus === FALSE || $BusinessStatus === FALSE){
                $this->BusinessModel->rollback();
                $this->PostModel->rollback();
                $this->error('发帖失败');
                exit;
            }else{
                $this->BusinessModel->commit();
                $this->PostModel->commit();
                $this->success('发帖成功','/pages/post/info',['postid' => $this->PostModel->id]);
                exit;
            }
        }
    }

    
    // 社区首页
    public function index(){
        if($this->request->isPost()){
            $page = $this->request->param('page',1,'trim');
            $cateid = $this->request->param('cateid',0,'trim');
            $keywords = $this->request->param('keywords','','trim');
            $limit = 10;

            $offset = ($page-1) * $limit;

            $where = [];

            if($cateid){
                $where['cateid'] = $cateid;
            }

            if($keywords){
                $where['title'] = ['like',"%$keywords%"];
            }

            $list = $this->PostModel
                ->with(['cate','business'])
                ->where($where)
                ->order('id','desc')
                ->limit($offset,$limit)
                ->select();

            if($list){
                $this->success('返回数据帖子',null,$list);
                exit;
            }else{
                $this->error('暂无更多数据');
                exit;
            }
        }
    }


    // 帖子详情
    public function info(){
        if($this->request->isPost()){
            $postid = $this->request->param('postid',0,'trim');
            $busid = $this->request->param('busid',0,'trim');

            // 查询帖子是否存在
            $post = $this->PostModel->with(['cate','business'])->find($postid);
            if(!$post){
                $this->error('帖子信息未知');
                exit;
            }

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if($business){
                // 查询收藏的状态
                $collect = $this->CollectModel->where(['postid' => $postid,'busid' => $busid])->find();

                if($collect){
                    // 追加自定义属性
                    $post['collect'] = true;
                }else{
                    $post['collect'] = false;
                }
            }

            $this->success('返回帖子信息',null,$post);
            exit;
        }
    }

    
    // 修改帖子
    public function edit(){
        if($this->request->isPost()){
            $postid = $this->request->param('postid',0,'trim');
            $busid = $this->request->param('busid',0,'trim');
            $cateid = $this->request->param('cateid',0,'trim');
            $content = $this->request->param('content','','trim');
            $point = $this->request->param('point',0,'trim');
            $title = $this->request->param('title','','trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;  
            }

            // 查询帖子
            $post = $this->PostModel->find($postid);
            if(!$post){
                $this->error('帖子信息未知');
                exit;
            }

            // 先判断是增加了悬赏积分还是减少
            if($post['point'] > $point){
                // 减少了,则提示用户不能小于初始悬赏积分
                $this->error('修改的悬赏积分不能低于初始悬赏积分');
                exit;
            }else{
                // 这个是补的差价
                $Subpoint = bcsub($point,$post['point']);
                
                // 判断余额是否充足
                $Updatepoint = bcsub($business['point'],$Subpoint);
                if($Updatepoint < 0){
                    $this->error('余额不足');
                }
            }

            // 开启事务
            $this->PostModel->startTrans();
            $this->BusinessModel->startTrans();

            $PostData = [
                'id' => $postid,
                'title' => $title,
                'content' => $content,
                'point' => $point,
                'cateid' => $cateid,
            ];

            $PostStatus = $this->PostModel->isUpdate(true)->save($PostData);

            if($PostStatus === FALSE){
                $this->error($this->PostModel->getError());
                exit;
            }

            $BusinessData = [
                'id' => $busid,
                'point' => $Updatepoint
            ];

            $BusinessStatus = $this->BusinessModel->isUpdate(true)->save($BusinessData);

            if($BusinessStatus === FALSE){
                $this->PostModel->rollback();
                $this->error($this->BusinessModel->getError());
            }

            if($PostStatus === FALSE || $BusinessStatus === FALSE){
                $this->BusinessModel->rollback();
                $this->PostModel->rollback();
                $this->error('修改帖子失败');
                exit;
            }else{
                $this->BusinessModel->commit();
                $this->PostModel->commit();
                $this->success('修改帖子成功');
                exit;
            }
        }
    }


    // 收藏
    public function collect(){
        if($this->request->isPost()){
            $postid = $this->request->param('postid',0,'trim');
            $busid = $this->request->param('busid',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询这个是否有点赞过
            $where = ['busid' => $busid, 'postid' => $postid];

            $collect = $this->CollectModel->where($where)->find();

            if($collect){
                // 有收藏过就要删除该记录
                $result = $this->CollectModel->where(['id' => $collect['id']])->delete();

                if($result === FALSE){
                    $this->error('取消收藏失败');
                    exit;
                }else{
                    $this->success('取消收藏成功');
                    exit;
                }
            }else{
                // 没有收藏过，增加数据
                $data = [
                    'busid' => $busid,
                    'postid' => $postid
                ];

                $result = $this->CollectModel->save($data);
                if($result === FALSE){
                    $this->error('收藏失败');
                    exit;
                }else{
                    $this->success('收藏成功');
                    exit;
                }
            }
        }
    }


    // 采纳
    public function accept(){
        if($this->request->isPost()){
            $comid = $this->request->param('comid',0,'trim');
            $postid = $this->request->param('postid',0,'trim');
            $busid = $this->request->param('busid',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询帖子
            $post = $this->PostModel->find($postid);
            if(!$post){
                $this->error('帖子信息未知');
                exit;
            }

            // 查询评论
            $comment = $this->CommentModel->find($comid);
            if(!$comment){
                $this->error('评论信息未知');
                exit;
            }

            // 采纳人的信息
            $acceptid = isset($comment['busid']) ? $comment['busid'] : 0;

            $accept = $this->BusinessModel->find($acceptid);
            if(!$accept){
                $this->error('采纳人未知');
                exit;
            }

            // 采纳人不能为自己
            if($acceptid == $busid){
                $this->error('回答不能采纳自己的');
                exit;
            }

            // 帖子表、评论表、用户表
            // 开启事务
            $this->PostModel->startTrans();
            $this->CommentModel->startTrans();
            $this->BusinessModel->startTrans();

            $PostData = [
                'id' => $postid,
                'status' => 1,
                'accept' => $acceptid
            ];

            $PostStatus = $this->PostModel->isUpdate(true)->save($PostData);

            if($PostStatus === FALSE){
                $this->error($this->PostModel->getError());
                exit;
            }

            $CommentData = [
                'id' => $comid,
                'status' => 1
            ];

            $CommentStatus = $this->CommentModel->isUpdate(true)->save($CommentData);

            if($CommentStatus === FALSE){
                $this->PostModel->rollback();
                $this->error($this->CommentModel->getError());
                exit;
            }

            $BusinessData = [
                'id' => $accept['id'],
                'point' => bcadd($accept['point'],$post['point'])
            ];

            $BusinessStatus = $this->BusinessModel->isUpdate(true)->save($BusinessData);

            if($BusinessStatus === FALSE){
                $this->CommentModel->rollback();
                $this->PostModel->rollback();
                $this->error($this->BusinessModel->getError());
                exit;
            }

            if($BusinessStatus === FALSE || $CommentStatus === FALSE || $PostStatus === FALSE){
                $this->BusinessModel->rollback();
                $this->CommentModel->rollback();
                $this->PostModel->rollback();
                $this->error('采纳评论失败');
                exit;
            }else{
                $this->BusinessModel->commit();
                $this->CommentModel->commit();
                $this->PostModel->commit();
                $this->success('采纳评论成功');
                exit;
            }

        }
    }


}
